<?php
namespace Home\Controller;

use Think\Controller;
set_time_limit(0);
class CheckController extends Controller {
    public function index()
    {
        $where = array(
            'expressNumber'=>array('neq',''),
            //'expressTime'  => array('neq',0),
            'isSign'       => 0
        );
        $checkData = D('ApiOrdersNew')->where($where)->select();

        foreach($checkData as $check) {

            $express = $this->express($check['expressNumber']);

            if(empty($express)) {

                echo $check['userName'].'--'.$check['expressNumber'].'--未发货'."\n";
                sleep(10);
                continue;
            }
            $time = strtotime($express['time']);
            //echo $check['userName'] .'--'.$time .'--'.$check['expressTime']."\n";
            if($check['expressTime'] != $time) {
                $log =$check['expressLog']."\n".$express['time'].'--'. $express['context'];
                $save = array(
                    'expressTime' => $time,
                    'expressLog'  => $log,
                );
                if(strpos($express['context'], '签收')) {
                    $save['isSign'] = 1;
                }
                D('ApiOrdersNew')->where(array('id' => $check['id']))->save($save);
                echo $check['userName'].'--'.$check['expressNumber'].'--'.$express['context']."\n";
                sleep(10);
            } else {
                //echo $check['userName'].'--'.$check['expressNumber'].'--未变化'."\n";
                sleep(10);
            }
        }
    }

    private function express($number,$post='',$cookie='', $returnCookie=0)
    {
            $url = "http://www.kuaidi100.com/query?type=zhongtong&postid=$number&id=1";
            //echo $url."\n";
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36');
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
            curl_setopt($curl, CURLOPT_REFERER, "http://XXX");
            if($post) {
                curl_setopt($curl, CURLOPT_POST, 1);
                curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
            }
            if($cookie) {
                curl_setopt($curl, CURLOPT_COOKIE, $cookie);
            }
            curl_setopt($curl, CURLOPT_HEADER, $returnCookie);
            curl_setopt($curl, CURLOPT_TIMEOUT, 10);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            $data = curl_exec($curl);
            if (curl_errno($curl)) {
                return curl_error($curl);
            }
            curl_close($curl);
            if($returnCookie){
                list($header, $body) = explode("\r\n\r\n", $data, 2);
                preg_match_all("/Set\-Cookie:([^;]*);/", $header, $matches);
                $info['cookie']  = substr($matches[1][0], 1);
                $info['content'] = $body;
                return $info;
            }else{
                $result = json_decode($data, TRUE);

                return $result['data'][0];
            }

        /*$type = 'zhongtong';
        $url = "http://www.kuaidi100.com/query?type=$type&postid=$number&id=1&valicode=&temp=0.9964493427759196";
        $res = file_get_contents($url);
        $result = json_decode($res, TRUE);
        var_dump($result);
        return $result['data'][0];*/
    }
}